# coding: utf-8

'''
# open service wiki: 
http://wiki.it.yzs.io:8090/pages/viewpage.action?pageId=24735552
'''

''
import time
import urllib2

from utils.req import *
from utils.case import Case
from utils.jktest import *
from test_case.open_service_data import *

def run(case_list):
    for case in case_list:
        run_case(case)

def run_case(case):
    # post support header and body define
    begin = time.time()
    if case.method == 'get':
        case.ret = send_get(case.url, case.params)
    elif case.method == 'post':
        if case.params.has_key("header") and case.params.has_key("body"):
            case.body = case.params["body"]
            case.header = case.params["header"]
            case.ret = send_post(case.url, case.body, headers=case.header)
        else:
            case.ret = send_post(case.url, case.params)
    
    case.resp_time = (time.time() - begin) * 1000
    print "\tresp_time: %.2f(ms) >> %s" % (case.resp_time, case.api)
    unmatchs = get_unmatch_list(case.checks, case.ret)
    if unmatchs:
        case.fail = True
        case.unmatchs = unmatchs

def init_case(api, data):
    case = Case()
    case.api = api
    case.method = data['method'].lower()
    case.url = Service_Interface + data['path']
    case.params = data['input']
    case.checks = data['output']
    return case

def check_single_api(api, data):
    case = init_case(api, data)
    run_case(case)
    output([case])

def check_all_api(test_data):
    case_list = []
    for api, data in test_data.items():
        case = init_case(api, data)
        case_list.append(case)
    run(case_list)
    output(case_list)

def output(case_list):
    total = len(case_list)
    fail = 0
    fail_log = 'results/open_service_failed.log'
    pass_log = 'results/open_service_pass.log'
    f_pass = open(pass_log, 'w')
    f_fail = open(fail_log, 'w')

    for case in case_list:
        if case.fail:
            f_fail.write("API:%s\n%s\n期望结果:%s\n\n不匹配:%s\n返回结果:%s\n\n" % \
                (case.api, case.url, case.checks, case.unmatchs, case.ret))
            f_fail.write("++++" * 10 + "\n")
            fail += 1
        else:
            f_pass.write("API:%s\n%s\n\n检查点: %s\n返回结果:%s\n\n" % \
                (case.api, case.url, case.checks, case.ret))
            f_pass.write("++++" * 10 + "\n")

    f_pass.close()
    f_fail.close()
    print "-" * 20
    print "Total: %d, Pass: %d Fail: %d" % (total, total-fail, fail)
    print "-" * 20
    if fail > 0:
        print ">>>> %s" % fail_log
    print ">>>> %s" % pass_log

if __name__ == '__main__':

    Server = "172.18.10.73:8080" # UAT
    # Server = "172.18.100.77:8080" # UAT SLB
    Service_Interface = "http://%s/open-service/" % Server

    # NLU 发布 / 取消; 体验测试接口
    Test_Data = NLU_TEST_DATA

    # 技能发布接口
    # Test_Data = USK_TEST_DATA
    
    # ASR 发布接口 : None
    # Test_Data = ASR_TEST_DATA
    # TTS 发布接口 : None
    # Test_Data = TTS_TEST_DATA

    # 体验测试
    # Test_Data = EXP_TEST_DATA
    # 7.1 ~ 7.5 无副作用
    # api = 'web.testAppNluOnline'
    # api = 'web.testAppAsr'
    # api = 'web.testAudioTrans'
    # api = 'web.testServiceNluOnline'
    # api = 'web.testTts'
    
    # api = 'web.publishDictV2'
    api = ''

    if api:
        check_single_api(api, Test_Data[api])
    else:
        check_all_api(Test_Data)
